<?php
namespace app\service;

use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\Cache;
use think\Log;
use think\Exception;
use think\Db;

class RedisDr extends Command
{
    protected function configure()
    {
        $this->setName('RedisDr')->setDescription('Here is the RedisDrService');
    }

    protected function execute(Input $input, Output $output)
    {
        $output->writeln('开始监听...');
        $redis = Cache::store('redis')->handler();
        $redis->config('notify-keyspace-events','Ex');
        $redis->setOption(\Redis::OPT_READ_TIMEOUT, -1);
        $redis->psubscribe(array('__keyevent@10__:expired'), 'app\service\RedisDr::key_callback');
    }
    public static function key_callback($redis, $pattern, $channel, $payload){
        $timenow = time();
        echo "Payload: $timenow: $payload\n";
        /*TODO处理业务逻辑*/
        $msginfo = explode(':', $payload);
        if(sizeof($msginfo) == 3){
            $env = $msginfo[0];
            if ($env != getenv('APP_NAME_EN')) {
                return;
            }
            $type = $msginfo[1];
            $value = $msginfo[2];
            switch ($type) {
                case 'alipush':
                    $str_param = str_replace("@#%#@&@##@!@#@&@@", ':', $value);
                    $params = json_decode($str_param, true);
                    if (!isset($params['alias']) || is_null($params['alias']) || !isset($params['title']) || is_null($params['title'])) break;
                    _rabbitMqPublish('action', [
                        'action' => 'api/push/send',
                        'data'   => $params
                    ]);
                    break;
                case 'rabbitMQReConsumption':
                    _rabbitMqPublish('ffmpeg', [
                        'id' => $value
                    ]);
                    break;
                case 'test':
                    _rabbitMqPublish('class_event', [
                        'class' => '\think\Cache',
                        'method'   => 'set',
                        'params'    => ['t'. $timenow . ':'. $value, '1', 120]
                    ]);
                    break;
                
                default:
                    break;
            }
        }
    }

}
